﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TCCVoigt.Models;

namespace TCCVoigt.Controllers
{
    public class RecursoController : Controller
    {
        private TCCVoigtEntities db = new TCCVoigtEntities();

        //
        // GET: /Funcao/

        public ActionResult Index()
        {
            return View(db.Recurso.ToList().OrderBy(p => p.Descricao));
        }

        //
        // GET: /Funcao/Details/5

        public ActionResult Details(int id = 0)
        {
            Recurso recurso = db.Recurso.Single(r => r.Codigo == id);
            if (recurso == null)
            {
                return HttpNotFound();
            }
            return View(recurso);
        }

        //
        // GET: /Funcao/Create

        public ActionResult Create()
        {
            ViewBag.CodigoFuncao = new SelectList(db.Funcao, "Codigo", "Descricao");
            return View();
        }

        //
        // POST: /Funcao/Create

        [HttpPost]
        public ActionResult Create(Recurso recurso)
        {
            if (ModelState.IsValid)
            {
                db.Recurso.AddObject(recurso);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CodigoFuncao = new SelectList(db.Funcao, "Codigo", "Descricao", recurso.CodigoFuncao);
            return View(recurso);
        }

        //
        // GET: /Funcao/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Recurso recurso = db.Recurso.Single(r => r.Codigo == id);
            if (recurso == null)
            {
                return HttpNotFound();
            }
            ViewBag.CodigoFuncao = new SelectList(db.Funcao, "Codigo", "Descricao", recurso.CodigoFuncao);
            return View(recurso);
        }

        //
        // POST: /Funcao/Edit/5

        [HttpPost]
        public ActionResult Edit(Recurso recurso)
        {
            if (ModelState.IsValid)
            {
                db.Recurso.Attach(recurso);
                db.ObjectStateManager.ChangeObjectState(recurso, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CodigoFuncao = new SelectList(db.Funcao, "Codigo", "Descricao", recurso.CodigoFuncao);
            return View(recurso);
        }

        //
        // GET: /Funcao/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Recurso recurso = db.Recurso.Single(f => f.Codigo == id);
            if (recurso == null)
            {
                return HttpNotFound();
            }
            return View(recurso);
        }

        //
        // POST: /Funcao/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Recurso recurso = db.Recurso.Single(r => r.Codigo == id);
            db.Recurso.DeleteObject(recurso);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        //
        // GET: /Funcao/Atitude/5

        public ActionResult Atitude(int id = 0)
        {
            return View(db.RecursoAtitude.ToList().Where(p => p.CodigoRecurso == id));
        }

        


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}